﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kunlun.Models;
using System.Xml;
using System.Xml.Linq;
using System.Text;

namespace Kunlun.BusinessLogic
{
    public class Kunlunphotolib
    {
        KunLunEntities entities = new KunLunEntities();
        public bool ModifyLib(Consumer_library lib)
        {
            lib.LastUpdateTime = DateTime.Now;
            entities.Entry(lib).State = EntityState.Modified;
            entities.SaveChanges();
            return true;
        }

        public bool AddLib(Consumer_library lib,int userid)
        {
            lib.LastUpdateTime = DateTime.Now;
            lib.BeginDate = DateTime.Now;
            lib.UserID = userid;
            entities.Consumer_library.Add(lib);
            entities.SaveChanges();
            return true;

        }
        public void DeleLib(int id)
        {
            Consumer_library lib = entities.Consumer_library.Single(l => l.ID == id);
            var pics = entities.Consumer_pic.Where(p => p.Library == lib.ID);
            foreach (Consumer_pic pic in pics)
            {
                pic.Status = 0;
            }
            //entities.Entry(pics).State = EntityState.Modified;
            entities.SaveChanges();
            lib.IsVisible = false;
            entities.Entry(lib).State = EntityState.Modified;
            entities.SaveChanges();

            

        }
        public string FlexXd(int userid )
        {
            StringBuilder xml = new StringBuilder();
            var Query = from c in entities.Consumer_library
                        join p in entities.Consumer_pic
                        on c.ID equals p.Library

                        where c.UserID == userid&& c.IsVisible==true
                        select new PhotoLib { ID = c.ID, LibName = c.LibraryName, ThumUrl = p.ThumUrl, ListUrl =p.ListPicUrl,ImageAlt=p.ImageAlt,PhotoID=p.ID,Width=p.Width,Height=p.Height};
            
           xml.Append("<Flex>");
           int libid = 0;
           foreach (PhotoLib lib in Query)
           {
               if (libid != lib.ID)
               {
                   if (libid == 0)
                   {
                       xml.Append(String.Format("<PhotoLib id=\"{0}\" description=\"{1}\" photo=\"{2}\" >\r\n", lib.ID, lib.LibName, WebLog.PhotoView + lib.ThumUrl));
                       libid = lib.ID;
                   }
                   else
                   {
                       xml.Append(String.Format("</PhotoLib><PhotoLib id=\"{0}\" description=\"{1}\" photo=\"{2}\">\r\n", lib.ID, lib.LibName, WebLog.PhotoView + lib.ThumUrl));
                       libid = lib.ID;
                   }    
                   
               }
               else
               {
                   xml.Append(String.Format("<Photo id=\"{0}\" s=\"{1}\"  m=\"{2}\" alt=\"{3}\" w=\"{4}\" h=\"{5}\"/>\r\n", lib.PhotoID, WebLog.PhotoView + lib.ThumUrl, WebLog.PhotoView + lib.ListUrl, lib.ImageAlt,lib.Width,lib.Height));
                       
               }
           }
           xml.Append("</PhotoLib></Flex>");
           return xml.ToString();
        }
        public string FlexPhoto(int userid,int id)
        {
            StringBuilder xml = new StringBuilder();
            var Query = from p in entities.Consumer_pic
                        where p.UserID == userid && p.Library == id && p.Status==1
                        select p;

                        

           // xml.Append("<Flex>");
            foreach (Consumer_pic lib in Query)
            {

                xml.Append(String.Format("<Photo id=\"{0}\" s=\"{1}\" m=\"{2}\" />", lib.ID, WebLog.PhotoView + lib.ThumUrl, WebLog.PhotoView + lib.ListPicUrl));
                 
            }
           // xml.Append("</Flex>");
            return xml.ToString();
        }
     
    }
  
    public class PhotoLib
    {
        public int ID { get; set; }
        public string LibName { get; set; }
        public string ThumUrl { get; set; }
        public string ListUrl { get; set; }
        public int PhotoID { get; set; }
        public  string ImageAlt{get;set;}
        public double Width { get; set; }
        public double Height { get; set; }
    }
    
}
